package cc.linker.steplink.crm.merchanth5.service.redis;

import cc.linker.steplink.crm.commons.utils.CalendarUtils;
import cc.linker.steplink.crm.merchanth5.dao.SignDao;
import cc.linker.steplink.crm.merchanth5.pojo.SignPo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @Author: CKQ
 * @DateTime: 2021/1/20 8:54
 * @Description: TODO
 */
@Slf4j
@Service
public class SignCacheService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private SignDao signDao;

    //抽成比例
    public static final String SIGN_INFO = "sign:org_";
    public static final String SIGN_CYCLE = "sign:cycle_org_";

    /**
     * @Description：获取机构下设置的
     * @DateTime：2021/1/20 9:00
     * @Params：
     * @Return：
     */
    public SignPo getSignCycleByOrgId(String orgId) {
        String signInfoStr = stringRedisTemplate.opsForValue().get(SIGN_INFO + orgId);
        if (StringUtils.isEmpty(signInfoStr)) {
            SignPo signPo = SignPo.builder().orgId(orgId).build();
            SignPo signInfo = this.signDao.getInfo(signPo);
            if (signInfo != null && !StringUtils.isEmpty(signInfo.getOrgId())) {
                stringRedisTemplate.opsForValue().set(SIGN_CYCLE + signInfo.getOrgId(), signInfo.getCycle());
                stringRedisTemplate.opsForValue().set(SIGN_INFO + orgId, JSON.toJSONString(signInfo, SerializerFeature.WriteMapNullValue));
            }
            return signInfo == null ? new SignPo() : signInfo;
        } else {
            return JSON.parseObject(signInfoStr, SignPo.class);
        }
    }

    /**
     * @Description：设置缓存或设置过期时间
     * @DateTime：2021/1/20 9:05
     * @Params： param signPo 签到的设置实体类
     * param isAdd  是否是添加
     * @Return：
     */
    public void setSignCache(SignPo signPo, Boolean isAdd) {
        if (isAdd) {
            stringRedisTemplate.opsForValue().set(SIGN_INFO + signPo.getOrgId(), JSON.toJSONString(signPo, SerializerFeature.WriteMapNullValue));
            stringRedisTemplate.opsForValue().set(SIGN_CYCLE + signPo.getOrgId(), signPo.getCycle());
        } else {
            String cycle = signPo.getCycle();
            String s = stringRedisTemplate.opsForValue().get(SIGN_CYCLE + signPo.getOrgId());
            if(!StringUtils.isEmpty(s)){
                cycle = s;
            }else{
                stringRedisTemplate.opsForValue().set(SIGN_CYCLE + signPo.getOrgId(), cycle);
            }
            signPo.setCycle(cycle);
            stringRedisTemplate.opsForValue().set(SIGN_INFO + signPo.getOrgId(), JSON.toJSONString(signPo, SerializerFeature.WriteMapNullValue));
            stringRedisTemplate.expire(SIGN_INFO + signPo.getOrgId(), CalendarUtils.getSecondsNextEarlyMorning(), TimeUnit.SECONDS);
            stringRedisTemplate.expire(SIGN_CYCLE + signPo.getOrgId(), CalendarUtils.getSecondsNextEarlyMorning(), TimeUnit.SECONDS);
        }
    }
}
